package cn.gok;

import java.io.*;
import java.util.Comparator;
import java.util.TreeSet;

class School implements Serializable {
    private String name;
    private String address;
    private int birthYear;

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", birthYear=" + birthYear +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getBirthYear() {
        return birthYear;
    }

    public void setBirthYear(int birthYear) {
        this.birthYear = birthYear;
    }

    public School(String name, String address, int birthYear) {
        this.name = name;
        this.address = address;
        this.birthYear = birthYear;
    }
}

class Student implements Comparable<Student> {
    private int sid;
    private String name;
    private int age;

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Student(int sid, String name, int age) {
        this.sid = sid;
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student student) {
        if (this.sid < student.sid) {
            return -1;
        } else if (this.sid == student.sid) {
            if (this.age < student.age) {
                return -1;
            }
            return 1;
        }
        return 1;
    }
}

class MyStudentComparator implements Comparator<Student> {

    @Override
    public int compare(Student s1, Student s2) {
        if (s1.getSid() < s2.getSid()) {
            return -1;
        } else if (s1.getSid() == s2.getSid()) {
            if (s1.getAge() < s2.getAge()) {
                return -1;
            }
            return 1;
        }
        return 1;
    }
}

public class homework1 {


    public static void main(String[] args) throws IOException {
        作业1();
        作业2();
    }

    private static void 作业1() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day5///1.dat"));
        School s1 = new School("泉州职业技术大学", "坑里", 2002);
        School s2 = new School("泉州体育职业学院", "泉州", 2010);
        oos.writeObject(s1);
        oos.writeObject(s2);
        oos.close();
        System.out.println("写入成功");

        ObjectInputStream ois =new ObjectInputStream(new FileInputStream("day5//1.dat"));
        try {
            while (true){

                Object obj=ois.readObject();
                System.out.println(obj);
            }
        } catch (EOFException | ClassNotFoundException e) {
            System.out.println("读取结束");
        }
        ois.close();

    }

    private static void 作业2() {
        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student(1, "一休", 18));
        set.add(new Student(2, "二郎神", 19));
        set.add(new Student(3, "三太子", 20));
        set.add(new Student(4, "四不像", 21));
        set.add(new Student(5, "五湖四海", 22));
        set.stream().forEach(System.out::println);
        System.out.println("输完结果了");
    }
}